package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderAdminService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单管理接口")

public class OrderAdminController {

    @Autowired
    private OrderAdminService orderAdminService;

    /**
     * 拒单
     */
    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result rejectOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("[拒单]ordersRejectionDTO:{}", ordersRejectionDTO);
        orderAdminService.rejectOrder(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 接单
     */
    @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result receiveOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("[接单]id:{}", ordersConfirmDTO);
        orderAdminService.receiveOrder(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 订单搜索
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> searchOrder(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("[订单搜索]ordersPageQueryDTO:{}", ordersPageQueryDTO);
        PageResult pageResult = orderAdminService.searchOrder(ordersPageQueryDTO);
        return Result.success(pageResult);

    }

    // 各个状态的订单数量统计
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        //调用
        OrderStatisticsVO orderStatisticsVO = orderAdminService.statistics();
        //响应
        return Result.success(orderStatisticsVO);
    }

    @GetMapping("details/{id}")
    @ApiOperation("根据id查询订单详情")
    public Result<OrderVO> getOrderDetails(@PathVariable Long id) {
        log.info("查询订单");
        OrderVO orderVO = orderAdminService.selectOrder(id);
        return Result.success(orderVO);

    }

    // 取消订单
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        //打印
        log.info("[取消订单]ordersCancelDTO:{}", ordersCancelDTO);
        //调用
        orderAdminService.cancle(ordersCancelDTO);
        //响应
        return Result.success();
    }

    //派送订单
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result delivery(@PathVariable Long id) {
        //打印
        log.info("[派送订单]id：{}", id);
        //调用
        orderAdminService.delivery(id);
        //响应
        return Result.success();
    }
    /**
     * 完成订单
     *
     * @return
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result complete(@PathVariable("id") Long id) {
        orderAdminService.complete(id);
        return Result.success();
    }
}
